package com.lemon.cloud.log;

import com.lemon.cloud.admin.feign.LogFeign;
import com.lemon.cloud.log.aspect.SysLogAspect;
import com.lemon.cloud.log.config.LogProperties;
import com.lemon.cloud.log.event.SysLogListener;
import com.lemon.cloud.log.intercepte.LogInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author huangds
 */
@EnableAsync
@Configuration(proxyBeanMethods = true)
@EnableConfigurationProperties(LogProperties.class)
@ConditionalOnProperty(value = "security.log.enabled", matchIfMissing = true)
public class LogAutoConfiguration implements WebMvcConfigurer {

	@Bean
	public SysLogListener sysLogListener(LogProperties logProperties, LogFeign logFeign) {
		return new SysLogListener(logFeign, logProperties);
	}

	@Bean
	public SysLogAspect sysLogAspect() {
		return new SysLogAspect();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(logInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns(
						"/static/**",
						"/css/**",
						"/js/**",
						"/images/**",
						"/webjars/**",
						"/favicon.ico",
						"/health",
						"/public/**"
				);
	}

	@Bean
	public LogInterceptor logInterceptor() {
		return new LogInterceptor();
	}
}
